import {
    request
} from "../../request/index"

// pages/goods_list/index.js
Page({
    data: {
        tabs: [{
                id: 0,
                value: "综合",
                isActive: true
            },
            {
                id: 1,
                value: "销量",
                isActive: false
            },
            {
                id: 2,
                value: "价格",
                isActive: false
            }
        ],
        goodsList:[]
    },
    //接口要的参数
    QueryParams:{
        query:"",
        cid:"",
        pagenum:1,
        pagesize:10
    },
    totalPages:1,

    
    
    //发请求获取商品列表信息
    async getGoodsList(){
        const res = await request ({
            // url: 'https://www.fastmock.site/mock/5ef0094d6cded52f33526421d6071aa4/shopmall/search',
            url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/search',
            data:this.QueryParams
        });
        //获取商品的总条数
        const total = res.data.message.total;
        //计算总页数
        this.totalPages = Math.ceil(total/this.QueryParams.pagesize);
        // let goodsList = res.data.message.goods
        this.setData({
            goodsList:[...this.data.goodsList,...res.data.message.goods]
        })
        //关闭下拉刷新的窗口
        wx.stopPullDownRefresh();
        
    },

    onLoad: function (options) {
        this.QueryParams.cid = options.cid

        this.getGoodsList();
    },
    //监听子组件 传值
    tabsItemChange(e) {
        const {index} = e.detail
        let tabs = JSON.parse(JSON.stringify(this.data.tabs))
        tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false);
        this.setData({
            tabs
        })
    },
    onReachBottom(){
        if(this.QueryParams.pagenum>=this.totalPages){
            wx.showToast({
                title: '没有下一页数据了',
                icon: 'none',
            });
        }else{
            this.QueryParams.pagenum++;
            this.getGoodsList()
        }
    },
    //下拉刷新时间
    onPullDownRefresh(){
        this.setData({
            goodsList:[]
        })

        //重置页码
        this.QueryParams.pagenum=1;
        //发送请求
        this.getGoodsList();
        
    }
})